<template>
  <view @touchmove.stop.prevent="" class="mask" :style="{opacity: opacity ? '1' : '0'}" v-if="focus">
    <view class="mask-content" :style="{bottom : bottom + 'px', opacity: opacity}">
      <view class="mask-content-topbar">
        <view class="left" @tap.stop="cancel">取消</view>
        <view class="right" @tap.stop="confirm">确定</view>
      </view>
      <view class="mask-content-input" @tap.stop.prevent="">
        <input
            class="textarea"
            v-model="value"
            :placeholder="placeholder"
            :focus="focus"
            :adjust-position="false"
            :hold-keyboard="true"
            @confirm="confirm"
            confirm-type="done"/>
        <text class="unit">{{unit}}</text>
      </view>
    </view>
  </view>
</template>

<script>
import {mapGetters} from 'vuex'
export default {
  name:"PopupInput",
  props:{
    placeholder: {
      type: String,
      default: '',
    },
    unit:{
      type: String,
      default: ''
    },
    isShow:{
      type:Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'text'
    },
  },
  data() {
    return {
      bottom: 0,
      opacity: 0,
      focus: false,
      value: '',
      showMask: false,
      timer: null,
    };
  },
  computed:{
    ...mapGetters(['inputValue'])
  },
  watch:{
    isShow(newValue){
      this.value = this.inputValue
      this.focus = newValue;
      if(newValue){
        this.showMask = true;
      }
      this.opacity =  1
      // uni.onKeyboardHeightChange(res=>{
      //   this.bottom =  res.height
      // })
      uni.onKeyboardHeightChange(this.getKeyboardHeight)
    },
  },
  onShow(){
    uni.onKeyboardHeightChange(this.getKeyboardHeight)
  },
  onHide(){
    clearTimeout(this.timer)
    //取消监听键盘高度，防止内存溢出
    uni.offKeyboardHeightChange(this.getKeyboardHeight)
  },
  methods:{
    getKeyboardHeight(res){
      this.bottom =  res.height
    },
    confirm(){
      this.timer = setTimeout(() => {
        this.showMask = false;
        this.$emit("confirm",this.value);
        this.$emit('close',false)
      }, 200)
    },
    cancel(){
      this.showMask = false;
      this.focus = false
      this.$emit("cancel",false)
      this.$emit('close',false)
    }
  },
  beforeDestroy(){
    clearTimeout(this.timer)
    //取消监听键盘高度，防止内存溢出
    uni.offKeyboardHeightChange(this.getKeyboardHeight)
  },
}
</script>

<style lang="scss" scoped>
.mask{
  display: flex;
  align-items: flex-end;
  position: fixed;
  left: 0;
  top: var(--window-top);
  bottom: 0;
  width: 100%;
  min-height: 200vh;
  background: rgba(0,0,0,0.5);
  z-index: 9995;
  .mask-content{
    position: fixed;
    width: 100%;
    background: #FFFFFF;
    // overflow-y:scroll;
    display: flex;
    flex-direction: column;
    .mask-content-topbar{
      display: flex;
      justify-content: space-between;
      padding: 30rpx 20rpx;
      font-size: 28rpx;
      .left{
        width: 300rpx;
        height: 60rpx;
        text-align: center;
        line-height: 60rpx;
        color: #2D8CF0;
        border: 2rpx solid #2D8CF0;
        border-radius: 5rpx;
      }
      .right{
        width: 300rpx;
        height: 60rpx;
        text-align: center;
        line-height: 60rpx;
        background-color: #2D8CF0;
        color: #ffffff;
        border: 2rpx solid #2D8CF0;
        border-radius: 5rpx;
      }
    }
    .mask-content-input{
      margin: 0rpx 20rpx 30rpx;
      border:2rpx solid #888888;
      display: flex;
      padding: 0 16rpx;
      border-radius: 10rpx;
      align-items: center;
      .textarea {
        flex: 1;
        height: 70rpx;
        font-size: 35rpx !important;
        color: black;
      }
    }
  }
}
</style>
